<?php
global $_GPC;
global $_W;
$cfg = $this->module['config'];
if ($_W['openid']) {
    header('location:' . $this->createMobileUrl('phone'));
    exit(0);
}
session_start();
$userid = $_GPC['_userid'];
if (!$userid) {
    header('location:' . $this->createMobileUrl('login'));
    exit(0);
}
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$user = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_user') . ' WHERE id =:a', array(':a' => $userid));
if (!$user || !$user['status']) {
    message('请添加门店和员工信息！', '', 'error');
}
$shop = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE id =:a', array(':a' => $user['pcate']));
$shopid = $shop['id'];
load()->model('mc');
if ($operation == 'display') {
    $categoryary = array();
    foreach ($tablepcatelist as $row) {
        $categoryary[$row['id']] = $row['title'];
    }
    $goodpcatelist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goodcategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' and status=1 order by  displayorder asc, id desc');
    $projectlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_projects') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' and status=1 order by  displayorder asc, id desc limit 5');
    $goodlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' and status=1 order by id desc limit 5');
    $staffcate = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_staffcate') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\'  and status=1 order by id desc ');
    $staffcateAry = array();
    foreach ($staffcate as $row) {
        $staffcateAry[$row['id']] = $row['title'];
    }
    $stafflist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_user') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and pcate=\'' . $shopid . '\'   order by id desc ');
    $staffAry = array();
    foreach ($stafflist as $row) {
        $staffAry[$row['id']] = $row['realname'];
    }
    $timecardcategory = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_timecardcategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' order by id desc');
    $projectslist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_projects') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by pcate desc,id desc ');
    $objary = array();
    foreach ($projectslist as $row) {
        $objary[$row['id']] = $row['title'];
    }
} else {
    if ($operation == 'logout') {
        isetcookie('_userid', '', -1);
        isetcookie('siteuniacid', '', -1);
        isetcookie('siteroot', '', -1);
        message('退出成功', $this->createMobileUrl('login'), 'success');
    } else {
        if ($operation == 'getproject') {
            $id = intval($_GPC['id']);
            $gtype = intval($_GPC['gtype']);
            $keyword = trim($_GPC['keyword']);
            if (!$id && !$keyword) {
                exit(json_encode(array('success' => false, 'msg' => '请输入id号')));
            }
            if ($id) {
                $item = array();
                if ($gtype == 1) {
                    $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_projects') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $id));
                } else {
                    $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $id));
                }
                if (!$item) {
                    exit(json_encode(array('success' => false, 'msg' => '无此商品')));
                }
                exit(json_encode(array('success' => true, 'item' => $item)));
            }
        } else {
            if ($operation == 'getstaff') {
                $stype = intval($_GPC['stype']);
                if (!$stype) {
                    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_user') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and pcate=\'' . $shopid . '\' and status=1 order by id desc ');
                } else {
                    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_user') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and pcate=\'' . $shopid . '\' and staffcate=:a and status=1 order by id desc ', array(':a' => $stype));
                }
                array_push($list, array('id' => 0, 'realname' => '无'));
                exit(json_encode(array('success' => true, 'list' => $list, 'a' => $shopid)));
            } else {
                if ($operation == 'neworder') {
                    $orderDisplayorder = pdo_fetchcolumn('SELECT max(CONVERT(`serialnum`,SIGNED))  FROM ' . tablename('j_hmoney_order') . ' WHERE weid=:c and groupid=:a and createdate=:b ', array(':a' => $shopid, ':b' => date('Y-m-d'), ':c' => $_W['uniacid']));
                    $orderDisplayorder = $orderDisplayorder + 1;
                    $data = array('weid' => $_W['uniacid'], 'groupid' => $shopid, 'userid' => $userid, 'serialnum' => $orderDisplayorder, 'out_trade_no' => TIMESTAMP . mt_rand(100, 999), 'createtime' => TIMESTAMP, 'createdate' => date('Y-m-d'));
                    pdo_insert('j_hmoney_order', $data);
                    $orderid = pdo_insertid();
                    exit(json_encode(array('success' => true, 'orderid' => $orderid, 'serialnum' => $orderDisplayorder)));
                } else {
                    if ($operation == 'getholdorderlist') {
                        $condition .= ' and groupid =\'' . $shopid . '\' ';
                        $sql = 'SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE groupid =\'' . $shopid . '\' and status=0 and createdate=\'' . date('Y-m-d') . '\' order by id desc ';
                        $list = pdo_fetchall($sql);
                        exit(json_encode(array('success' => true, 'list' => $list)));
                    } else {
                        if ($operation == 'getorderdetail') {
                            $orderid = $_GPC['orderid'];
                            $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $orderid));
                            $goodlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . '  WHERE orderid=:a order by id desc ', array(':a' => $trade['id']));
                            exit(json_encode(array('success' => true, 'trade' => $trade, 'goods' => $goodlist)));
                        } else {
                            if ($operation == 'getorder') {
                                $orderid = $_GPC['orderid'];
                                $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $orderid));
                                $market = $this->findMarketing($trade['order_fee'] - $trade['notdiscount_fee'], $shopid, 1);
                                exit(json_encode(array('success' => true, 'trade' => $trade, 'market' => $market)));
                            } else {
                                if ($operation == 'clearorder') {
                                    $orderid = $_GPC['orderid'];
                                    $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE id=:a', array(':a' => $orderid));
                                    if ($trade['status'] || !$trade) {
                                        exit(json_encode(array('success' => false, 'msg' => '该订单不能删除')));
                                    }
                                    pdo_delete('j_hmoney_order', array('id' => $trade['id']));
                                    pdo_delete('j_hmoney_ordergoods', array('orderid' => $row['id']));
                                    exit(json_encode(array('success' => true)));
                                } else {
                                    if ($operation == 'deleteordergoods') {
                                        $mid = $_GPC['mid'];
                                        $goods = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE id=:a', array(':a' => $mid));
                                        if ($goods['ispay'] || !$goods) {
                                            exit(json_encode(array('success' => false, 'msg' => '该项目不能删除')));
                                        }
                                        pdo_delete('j_hmoney_ordergoods', array('id' => $mid));
                                        exit(json_encode(array('success' => true)));
                                    } else {
                                        if ($operation == 'holdorder') {
                                            $orderid = intval($_GPC['orderid']);
                                            $memberid = intval($_GPC['memberid']);
                                            $prama = trim($_GPC['goods']);
                                            if (!$orderid) {
                                                exit(json_encode(array('success' => false, 'msg' => '请先开单')));
                                            }
                                            $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE id=:a', array(':a' => $orderid));
                                            $pramaAry = explode(',', $prama);
                                            $num = 0;
                                            $order_fee = 0;
                                            foreach ($pramaAry as $row) {
                                                $tempary = explode('||', $row);
                                                $num += $tempary[4];
                                                $order_fee += intval($tempary[6] * 100);
                                                $datetemp = array('weid' => $_W['uniacid'], 'groupid' => $shopid, 'orderid' => $orderid, 'out_trade_no' => $trade['out_trade_no'], 'goodid' => $tempary[1], 'gtype' => $tempary[2], 'oldprice' => $tempary[3], 'price' => $tempary[3], 'num' => $tempary[4], 'discount' => intval($tempary[5]), 'total_fee' => intval($tempary[6] * 100), 'createtime' => TIMESTAMP, 'status' => 0, 'performance' => $tempary[7]);
                                                if (intval($tempary[0])) {
                                                    unset($datetemp['status']);
                                                    unset($datetemp['createtime']);
                                                    pdo_update('j_hmoney_ordergoods', $datetemp, array('id' => $tempary[0]));
                                                } else {
                                                    if ($tempary[2] == 1) {
                                                        $title = pdo_fetchcolumn('SELECT title FROM ' . tablename('j_hmoney_projects') . ' WHERE id=:a', array(':a' => $tempary[1]));
                                                    } else {
                                                        $title = pdo_fetchcolumn('SELECT title FROM ' . tablename('j_hmoney_goods') . ' WHERE id=:a', array(':a' => $tempary[1]));
                                                    }
                                                    $datetemp['title'] = $title;
                                                    pdo_insert('j_hmoney_ordergoods', $datetemp);
                                                }
                                            }
                                            $data = array('memberno' => $memberid, 'num' => $num, 'order_fee' => $order_fee, 'total_fee' => $order_fee);
                                            pdo_update('j_hmoney_order', $data, array('id' => $orderid));
                                            exit(json_encode(array('success' => true, 'orderid' => $orderid)));
                                        } else {
                                            if ($operation == 'searchgoods') {
                                                $gtpye = intval($_GPC['gtpye']);
                                                $keyword = trim($_GPC['keyword']);
                                                $limit = ' limit 8';
                                                if ($gtpye == 1) {
                                                    if ($keyword) {
                                                        $condition = ' and (title like \'%' . $keyword . '%\')';
                                                    }
                                                    $goodslist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_projects') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=:b and status=1 ' . $condition . ' order by displayorder asc,id desc ' . $limit, array(':b' => $shopid));
                                                } else {
                                                    if ($keyword) {
                                                        $condition = ' and (title like \'%' . $keyword . '%\' or goodsn like \'%' . $keyword . '%\' or shorttitle like \'%' . $keyword . '%\' )';
                                                    }
                                                    $goodslist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_goods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid=:b and status=1 and store<>0 ' . $condition . ' order by displayorder asc,id desc ' . $limit, array(':b' => $shopid));
                                                }
                                                exit(json_encode(array('success' => true, 'list' => $goodslist)));
                                            } else {
                                                if ($operation == 'searchmembers') {
                                                    $keyword = trim($_GPC['keyword']);
                                                    $limit = ' limit 8';
                                                    $condition = $keyword ? ' and (cardno like \'%' . $keyword . '%\' or wxcardno like \'%' . $keyword . '%\' or mobile like \'%' . $keyword . '%\' or realname like \'%' . $keyword . '%\' )' : ' ';
                                                    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_membercard') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and status=1 ' . $condition . ' order by id desc ' . $limit);
                                                    exit(json_encode(array('success' => true, 'list' => $list)));
                                                } else {
                                                    if ($operation == 'searchonemember') {
                                                        $keyword = trim($_GPC['keyword']);
                                                        $orderid = trim($_GPC['orderid']);
                                                        if (!$keyword) {
                                                            exit(json_encode(array('success' => false, 'msg' => '请输入关键字')));
                                                        }
                                                        $condition = $keyword ? ' and (cardno like \'%' . $keyword . '%\' or wxcardno like \'%' . $keyword . '%\' or mobile like \'%' . $keyword . '%\' or realname like \'%' . $keyword . '%\' )' : ' ';
                                                        $member = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_membercard') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and status=1 ' . $condition . ' order by id desc ');
                                                        if (!$member) {
                                                            exit(json_encode(array('success' => false, 'msg' => '无此会员')));
                                                        }
                                                        $member['cash2'] = $member['cash'];
                                                        if ($member['birdday']) {
                                                        } else {
                                                        }
                                                        $member['bird'] = date('m-d', strtotime($member['birdday']));
                                                        $member['credit2'] = $member['credit'];
                                                        $groupAry = array();
                                                        mc_groups();
                                                        foreach (mc_groups() as $row) {
                                                            $groupAry[$row['groupid']] = $row['title'];
                                                        }
                                                        $member['level'] = $groupAry[$member['gid']];
                                                        if ($member['uid'] || $member['openid']) {
                                                            $uid = $member['uid'];
                                                            if (!$uid) {
                                                                $uid = mc_openid2uid($member['openid']);
                                                            }
                                                            $memberuser = mc_fetch($uid);
                                                            if ($cfg['isgeneralbalance']) {
                                                                bcmul($memberuser['credit2'], 100, 0);
                                                                $member['cash2'] = $member['cash2'] + bcmul($memberuser['credit2'], 100, 0);
                                                            }
                                                            $member['credit2'] = $member['credit'] + $memberuser['credit1'];
                                                        }
                                                        $member['cash2'] = $this->_2money($member['cash2']);
                                                        if ($orderid) {
                                                            $goodlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . '  WHERE orderid=:a and gtype=1 order by id desc ', array(':a' => $orderid));
                                                            $cardlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_timecardorder') . '  WHERE memberid=:b and pid in (SELECT goodid FROM ' . tablename('j_hmoney_ordergoods') . '  WHERE orderid=:a and gtype=1 ) and usetime=0 and status=0 order by id asc ', array(':a' => $orderid, ':b' => $member['id']));
                                                            if ($cardlist) {
                                                                $cardary = array();
                                                                foreach ($cardlist as $row) {
                                                                    if (!isset($cardary[$row['pid']])) {
                                                                        $cardary[$row['pid']] = 0;
                                                                    }
                                                                    $cardary[$row['pid']] = $cardary[$row['pid']] + 1;
                                                                }
                                                                $goodAry = array();
                                                                foreach ($goodlist as $row) {
                                                                    if (!isset($cardary[$row['goodid']]) || $cardary[$row['goodid']] == 0) {
                                                                        continue;
                                                                    } else {
                                                                        if ($cardary[$row['goodid']] > 0) {
                                                                            $cardary[$row['goodid']] = $cardary[$row['goodid']] - 1;
                                                                            $goodAry[$row['id']] = $row;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        exit(json_encode(array('success' => true, 'member' => $member, 'timecard' => $goodAry)));
                                                    } else {
                                                        if ($operation == 'getmember') {
                                                            $memberid = intval($_GPC['memberid']);
                                                            $member = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_membercard') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $memberid));
                                                            if (!$member) {
                                                                exit(json_encode(array('success' => false, 'msg' => '无此会员')));
                                                            }
                                                            $member['cash2'] = $member['cash'];
                                                            if ($member['birdday']) {
                                                            } else {
                                                            }
                                                            $member['bird'] = date('m-d', strtotime($member['birdday']));
                                                            $member['credit2'] = $member['credit'];
                                                            $groupAry = array();
                                                            mc_groups();
                                                            foreach (mc_groups() as $row) {
                                                                $groupAry[$row['groupid']] = $row['title'];
                                                            }
                                                            $member['level'] = $groupAry[$member['gid']];
                                                            if ($member['uid'] || $member['openid']) {
                                                                $uid = $member['uid'];
                                                                if (!$uid) {
                                                                    $uid = mc_openid2uid($member['openid']);
                                                                }
                                                                $memberuser = mc_fetch($uid);
                                                                if ($cfg['isgeneralbalance']) {
                                                                    bcmul($memberuser['credit2'], 100, 0);
                                                                    $member['cash2'] = $member['cash2'] + bcmul($memberuser['credit2'], 100, 0);
                                                                }
                                                                $member['credit2'] = $member['credit'] + $memberuser['credit1'];
                                                            }
                                                            $member['cash2'] = $this->_2money($member['cash2']);
                                                            exit(json_encode(array('success' => true, 'member' => $member)));
                                                        } else {
                                                            if ($operation == 'payall') {
                                                                $orderid = intval($_GPC['orderid']);
                                                                if (!$orderid) {
                                                                    exit(json_encode(array('success' => false, 'msg' => '订单号不能为空')));
                                                                }
                                                                $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE id =:a', array(':a' => $orderid));
                                                                if ($trade['ispay'] > 0) {
                                                                    exit(json_encode(array('success' => false, 'msg' => '已经支付的订单不能再支付')));
                                                                }
                                                                $data = array('total_fee' => intval($_GPC['totalfee'] * 100), 'paid_fee' => intval($_GPC['totalfee'] * 100), 'discount' => $_GPC['discount'], 'discount_fee' => intval($_GPC['coupon'] * 100), 'discounttype' => $_GPC['coupontype'], 'couponid' => $_GPC['couponno'], 'memberno' => $_GPC['memberid'], 'paytype' => $_GPC['paytype'], 'paytype2' => $_GPC['paytype2'], 'remark' => $_GPC['remark'], 'marketing' => $_GPC['marketing'], 'change_fee' => bcmul($_GPC['change'], 100, 0), 'paid_fee1' => intval($_GPC['paidfee'] * 100), 'paid_fee2' => intval($_GPC['paidfee2'] * 100), 'timecardgid' => $_GPC['timecard']);
                                                                if ($data['timecardgid'] && $data['total_fee'] == 0) {
                                                                    $data['paytype'] = 2;
                                                                    $data['paytype2'] = -1;
                                                                    $data['paid_fee1'] = 0;
                                                                    $data['paid_fee2'] = 0;
                                                                }
                                                                if ($data['paid_fee1'] + $data['paid_fee2'] > $data['total_fee']) {
                                                                    if ($data['paytype'] == 2) {
                                                                        $data['paid_fee1'] = $data['total_fee'] - $data['paid_fee2'];
                                                                    }
                                                                    if ($data['paytype2'] == 2) {
                                                                        $data['paid_fee2'] = $data['total_fee'] - $data['paid_fee1'];
                                                                    }
                                                                }
                                                                if ($data['couponid']) {
                                                                    $employCard = $this->employCard($data['couponid'], $shopid);
                                                                    if (!$employCard || !$employCard['success']) {
                                                                        exit(json_encode(array('success' => false, 'msg' => $employCard['msg'])));
                                                                    }
                                                                }
                                                                if (!$data['paid_fee2']) {
                                                                    $data['paytype2'] = -1;
                                                                    $data['pay2_status'] = 1;
                                                                }
                                                                pdo_update('j_hmoney_order', $data, array('id' => $orderid));
                                                                if ($data['paytype'] > 4 || $data['paytype2'] > 4) {
                                                                    $data['ispay'] = 1;
                                                                    $data['time_end'] = TIMESTAMP;
                                                                    if ($data['paytype'] > 4) {
                                                                        $data['pay1_status'] = 1;
                                                                    }
                                                                    if ($data['paytype2'] > 4) {
                                                                        $data['pay2_status'] = 1;
                                                                    }
                                                                }
                                                                if ($data['paytype'] == 2 || $data['paytype2'] == 2) {
                                                                    $data['ispay'] = 1;
                                                                    $data['time_end'] = TIMESTAMP;
                                                                    if ($data['paytype'] == 2) {
                                                                        $data['pay1_status'] = 1;
                                                                    }
                                                                    if ($data['paytype2'] == 2) {
                                                                        $data['pay2_status'] = 1;
                                                                    }
                                                                }
                                                                if ($data['paytype'] == 3 || $data['paytype2'] == 3) {
                                                                    $data['ispay'] = 1;
                                                                    $data['time_end'] = TIMESTAMP;
                                                                    $data['remark'] = $data['remark'] . ';银行卡' . $_GPC['code'];
                                                                    if ($data['paytype'] == 3) {
                                                                        $data['pay1_status'] = 1;
                                                                    }
                                                                    if ($data['paytype2'] == 3) {
                                                                        $data['pay2_status'] = 1;
                                                                    }
                                                                }
                                                                if ($data['paytype'] == 4 || $data['paytype2'] == 4) {
                                                                    if (!$data['memberno']) {
                                                                        exit(json_encode(array('success' => false, 'msg' => '会员卡不能为空')));
                                                                    }
                                                                    $member = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_membercard') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $data['memberno']));
                                                                    if (!$member) {
                                                                        exit(json_encode(array('success' => false, 'msg' => '会员数据错误')));
                                                                    }
                                                                    if ($cfg['ispaycode']) {
                                                                        if (!$_GPC['paycode']) {
                                                                            exit(json_encode(array('success' => false, 'msg' => '验证码不能为空')));
                                                                        }
                                                                        if ($_GPC['paycode'] != $trade['paycode']) {
                                                                            exit(json_encode(array('success' => false, 'msg' => '验证码错误')));
                                                                        }
                                                                    }
                                                                    $membercash = $member['cash'];
                                                                    $isGeneral = 0;
                                                                    if ($member['openid'] && $cfg['isgeneralbalance']) {
                                                                        if ($member['cash']) {
                                                                            $this->transferBalance($member['id']);
                                                                        }
                                                                        $uid = $member['uid'];
                                                                        if (!$uid) {
                                                                            $uid = mc_openid2uid($member['openid']);
                                                                        }
                                                                        $memberinfo = mc_fetch($uid);
                                                                        $membercash = intval($memberinfo['credit2'] * 100);
                                                                        $isGeneral = 1;
                                                                    }
                                                                    $payfee = $data['paytype'] == 4 ? $data['paid_fee1'] : $data['paid_fee2'];
                                                                    if ($membercash < $payfee) {
                                                                        exit(json_encode(array('success' => false, 'msg' => '余额不足')));
                                                                    }
                                                                    if ($isGeneral) {
                                                                        $this->_2money($payfee);
                                                                        mc_credit_update($uid, 'credit2', $this->_2money($payfee) * -1, array(0 => '', 1 => '收银系统支付￥' . $this->_2money($payfee) . '元'));
                                                                    } else {
                                                                        pdo_update('j_hmoney_membercard', array('cash' => $member['cash'] - $payfee), array('id' => $member['id']));
                                                                    }
                                                                    $this->memberCardInform($data['memberno']);
                                                                    $data['ispay'] = 1;
                                                                    $data['time_end'] = TIMESTAMP;
                                                                    if ($data['paytype'] == 4) {
                                                                        $data['pay1_status'] = 1;
                                                                    }
                                                                    if ($data['paytype2'] == 4) {
                                                                        $data['pay2_status'] = 1;
                                                                    }
                                                                    $memberdata = array('weid' => $_W['uniacid'], 'cid' => $member['id'], 'userid' => $userid, 'groupid' => $shopid, 'usetype' => 1, 'paytype' => -1, 'startfee' => $membercash, 'cash' => $payfee, 'endfee' => $membercash - $payfee, 'out_trade_no' => $trade['out_trade_no'], 'status' => 1, 'createtime' => TIMESTAMP, 'createdate' => date('Y-m-d'));
                                                                    pdo_insert('j_hmoney_recharge', $memberdata);
                                                                }
                                                                if ($data['paytype'] == 0 || $data['paytype2'] == 0) {
                                                                    $qrcode = $_GPC['qrcode'];
                                                                    $payfee = $data['paytype'] == 0 ? $data['paid_fee1'] : $data['paid_fee2'];
                                                                    if ($cfg['isbank']) {
                                                                        $bank_trade_no = TIMESTAMP . mt_rand(1000, 9999);
                                                                        pdo_update('j_hmoney_order', array('bank_trade_no' => $bank_trade_no), array('id' => $orderid));
                                                                        $result = Bankpay_micropay($cfg, $payfee, $bank_trade_no, $trade['out_trade_no'], $qrcode, $userid, $shopid);
                                                                        if (isset($result['success']) && $result['success'] == false) {
                                                                            exit(json_encode(array('success' => false, 'msg' => $result['msg'])));
                                                                        }
                                                                        if ($result['status'] || $result['result_code']) {
                                                                            if ($result['err_code'] == 10003 || $result['err_code'] == 'USERPAYING') {
                                                                                exit(json_encode(array('success' => true, 'paywait' => true, 'orderid' => $orderid)));
                                                                            }
                                                                            exit(json_encode(array('success' => false, 'msg' => $result['message'] . $result['err_msg'])));
                                                                        }
                                                                        $data['openid'] = $result['sub_openid'];
                                                                        $data['cash_fee'] = $result['total_fee'];
                                                                        $data['transaction_id'] = $result['out_transaction_id'];
                                                                        $data['time_end'] = strtotime($result['time_end']);
                                                                        $data['ispay'] = 1;
                                                                    } else {
                                                                        if ($cfg['isymf']) {
                                                                            $payfee1 = $this->_2money($payfee);
                                                                            $result = ymf_micropay($qrcode, $payfee1, $trade['id'], $cfg);
                                                                            if ($result['success']) {
                                                                                $data['bank_trade_no'] = $result['orderno'];
                                                                                $data['openid'] = $result['uuid'];
                                                                                $data['cash_fee'] = intval($result['money'] * 100);
                                                                                $data['transaction_id'] = $result['transid'];
                                                                                $data['time_end'] = strtotime($result['paytime']);
                                                                                $data['ispay'] = 1;
                                                                            } else {
                                                                                exit(json_encode(array('success' => false, 'msg' => $result['msg'])));
                                                                            }
                                                                        } else {
                                                                            $payParama = array('appid' => $shop['appid'] ? $shop['appid'] : $cfg['appid'], 'pay_mchid' => strval($shop['pay_mchid']) ? strval($shop['pay_mchid']) : strval($cfg['pay_mchid']), 'pay_signkey' => $shop['pay_signkey'] ? $shop['pay_signkey'] : $cfg['pay_signkey'], 'outTradeNo' => strval($trade['out_trade_no']), 'pay_ip' => $shop['pay_ip'] ? $shop['pay_ip'] : $cfg['pay_ip'], 'sub_appid' => $shop['sub_appid'] ? $shop['sub_appid'] : $cfg['sub_appid'], 'sub_mch_id' => $shop['sub_mch_id'] ? $shop['sub_mch_id'] : $cfg['sub_mch_id']);
                                                                            $pay_signkey = trim($shop['pay_signkey']) ? trim($shop['pay_signkey']) : trim($cfg['pay_signkey']);
                                                                            $pageparama = array('appid' => $payParama['appid'], 'mch_id' => strval($shop['pay_mchid']) ? strval($shop['pay_mchid']) : strval($cfg['pay_mchid']), 'device_info' => $deviceinfo, 'nonce_str' => getNonceStr(), 'body' => $shop['companyname'], 'detail' => $shop['companyname'], 'attach' => '-', 'out_trade_no' => $payParama['outTradeNo'], 'total_fee' => $payfee, 'fee_type' => 'CNY', 'spbill_create_ip' => $shop['pay_ip'] ? $shop['pay_ip'] : $cfg['pay_ip'], 'goods_tag' => '000001', 'auth_code' => $qrcode);
                                                                            if ($payParama['sub_appid']) {
                                                                                $pageparama['sub_appid'] = $payParama['sub_appid'];
                                                                            }
                                                                            if ($payParama['sub_mch_id']) {
                                                                                $pageparama['sub_mch_id'] = $payParama['sub_mch_id'];
                                                                            }
                                                                            $sign = MakeSign($pageparama, $pay_signkey);
                                                                            $pageparama['sign'] = $sign;
                                                                            $xml = ToXml($pageparama);
                                                                            $response = postXmlCurl($xml, 'https://api.mch.weixin.qq.com/pay/micropay', 10);
                                                                            $result = FromXml($response);
                                                                            if ($result['result_code'] != 'SUCCESS') {
                                                                                if ($result['err_code'] == 'USERPAYING') {
                                                                                    exit(json_encode(array('success' => true, 'paywait' => true, 'orderid' => $orderid)));
                                                                                } else {
                                                                                    if ($result['err_code'] == 'INVALID_REQUEST') {
                                                                                        if ($result['err_code_des'] == 201) {
                                                                                            $neworderno = TIMESTAMP . mt_rand(1, 999);
                                                                                            pdo_update('j_hmoney_order', array('out_trade_no' => $neworderno), array('id' => $orderid));
                                                                                            pdo_update('j_hmoney_ordergoods', array('out_trade_no' => $neworderno), array('orderid' => $orderid));
                                                                                            exit(json_encode(array('success' => false, 'msg' => '支付单号重复，请再次提交')));
                                                                                        }
                                                                                    }
                                                                                }
                                                                                exit(json_encode(array('success' => false, 'msg' => $result['err_code_des'] . $result['return_msg'])));
                                                                            }
                                                                            $data['openid'] = $result['openid'];
                                                                            $data['cash_fee'] = $result['cash_fee'];
                                                                            $data['coupon_fee'] = $result['coupon_fee'];
                                                                            $data['transaction_id'] = $result['transaction_id'];
                                                                            $data['time_end'] = strtotime($result['time_end']);
                                                                            $data['ispay'] = 1;
                                                                        }
                                                                    }
                                                                    if ($data['paytype'] == 0) {
                                                                        $data['pay1_status'] = 1;
                                                                    }
                                                                    if ($data['paytype2'] == 0) {
                                                                        $data['pay2_status'] = 1;
                                                                    }
                                                                }
                                                                if ($data['paytype'] == 1 || $data['paytype2'] == 1) {
                                                                    $qrcode = $_GPC['qrcode'];
                                                                    $payfee = $data['paytype'] == 1 ? $data['paid_fee1'] : $data['paid_fee2'];
                                                                    if ($cfg['isbank']) {
                                                                        $bank_trade_no = TIMESTAMP . mt_rand(1000, 9999);
                                                                        pdo_update('j_hmoney_order', array('bank_trade_no' => $bank_trade_no), array('id' => $orderid));
                                                                        $result = Bankpay_micropay($cfg, $payfee, $bank_trade_no, $trade['out_trade_no'], $qrcode, $userid, $shopid);
                                                                        if (isset($result['success']) && $result['success'] == false) {
                                                                            exit(json_encode(array('success' => false, 'msg' => $result['msg'])));
                                                                        }
                                                                        if ($result['status'] || $result['result_code']) {
                                                                            if ($result['err_code'] == 10003 || $result['err_code'] == 'USERPAYING') {
                                                                                exit(json_encode(array('success' => true, 'paywait' => true, 'orderid' => $orderid)));
                                                                            }
                                                                            exit(json_encode(array('success' => false, 'msg' => $result['message'] . $result['err_msg'])));
                                                                        }
                                                                        $data['openid'] = $result['sub_openid'];
                                                                        $data['cash_fee'] = $result['total_fee'];
                                                                        $data['transaction_id'] = $result['out_transaction_id'];
                                                                        $data['time_end'] = strtotime($result['time_end']);
                                                                        $data['ispay'] = 1;
                                                                    } else {
                                                                        if ($cfg['isymf']) {
                                                                            $payfee1 = $this->_2money($payfee);
                                                                            $result = ymf_micropay($qrcode, $payfee1, $trade['id'], $cfg);
                                                                            if ($result['success']) {
                                                                                $data['bank_trade_no'] = $result['orderno'];
                                                                                $data['openid'] = $result['uuid'];
                                                                                $data['cash_fee'] = intval($result['money'] * 100);
                                                                                $data['transaction_id'] = $result['transid'];
                                                                                $data['time_end'] = strtotime($result['paytime']);
                                                                                $data['ispay'] = 1;
                                                                            } else {
                                                                                exit(json_encode(array('success' => false, 'msg' => $result['msg'])));
                                                                            }
                                                                        } else {
                                                                            $payParama = array('app_id' => $shop['app_id'] ? $shop['app_id'] : $cfg['app_id'], 'outTradeNo' => strval($trade['out_trade_no']), 'appauthtoken' => $shop['appauthtoken'] ? $shop['appauthtoken'] : $cfg['appauthtoken'], 'alipay_cert' => $cfg['alipay_cert'], 'alipay_key' => $cfg['alipay_key'], 'alipay_store_id' => $shop['alipay_store_id'], 'alipid' => $shop['alipid'] ? $shop['alipid'] : $cfg['alipid']);
                                                                            $total_amount = $data['total_fee'];
                                                                            $postfee = $this->_2money($payfee);
                                                                            require_once '../addons/j_hmoney/plug/plug_alipay/F2fpay.php';
                                                                            $f2fpay = new F2fpay();
                                                                            $response = $f2fpay->barpay($payParama['outTradeNo'], $qrcode, $postfee, $shop['companyname'], $payParama);
                                                                            $temp = (array) $response;
                                                                            $result = (array) $temp['alipay_trade_pay_response'];
                                                                            if ($result['code'] == 10003) {
                                                                                exit(json_encode(array('success' => true, 'paywait' => true, 'orderid' => $orderid)));
                                                                            } else {
                                                                                if ($result['code'] == 10000) {
                                                                                    $data['transaction_id'] = $result['trade_no'];
                                                                                    $data['cash_fee'] = intval($result['receipt_amount'] * 100);
                                                                                    $data['time_end'] = strtotime($result['gmt_payment']);
                                                                                    $data['ispay'] = 1;
                                                                                } else {
                                                                                    exit(json_encode(array('success' => false, 'msg' => $result['sub_msg'], 'aa' => $payParama)));
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                    if ($data['paytype'] == 1) {
                                                                        $data['pay1_status'] = 1;
                                                                    }
                                                                    if ($data['paytype2'] == 1) {
                                                                        $data['pay2_status'] = 1;
                                                                    }
                                                                }
                                                                $data['status'] = 2;
                                                                pdo_update('j_hmoney_order', $data, array('id' => $orderid));
                                                                if ($data['timecardgid'] && $data['memberno']) {
                                                                    $goodlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . '  WHERE id in (' . $data['timecardgid'] . ') order by id desc ');
                                                                    foreach ($goodlist as $row) {
                                                                        $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_timecardorder') . '  WHERE memberid=:a and pid=:b and usetime=0 and status=0 order by id asc ', array(':a' => $data['memberno'], ':b' => $row['goodid']));
                                                                        if ($item) {
                                                                            pdo_update('j_hmoney_timecardorder', array('orderid' => $orderid, 'ordergoodid' => $row['id'], 'usetime' => TIMESTAMP, 'status' => 1, 'total_fee' => $row['total_fee'], 'title' => $row['title']), array('id' => $item['id']));
                                                                            pdo_update('j_hmoney_ordergoods', array('tcardid' => $item['id']), array('id' => $row['id']));
                                                                        }
                                                                    }
                                                                }
                                                                pdo_update('j_hmoney_ordergoods', array('ispay' => 1, 'status' => 2), array('orderid' => $orderid));
                                                                exit(json_encode(array('success' => true)));
                                                            } else {
                                                                if ($operation == 'paywaitpassword') {
                                                                    $orderid = intval($_GPC['orderid']);
                                                                    if (!$orderid) {
                                                                        exit(json_encode(array('success' => false, 'msg' => '订单号不能为空')));
                                                                    }
                                                                    $trade = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE id =:a', array(':a' => $orderid));
                                                                    if ($trade['ispay'] == 1) {
                                                                        exit(json_encode(array('success' => false, 'msg' => '已经支付的订单不能再支付')));
                                                                    }
                                                                    if ($cfg['isbank']) {
                                                                        $result = Bankpay_checkpay($cfg, $trade['bank_trade_no']);
                                                                        if (!$result['status'] && !$result['result_code']) {
                                                                            if ($result['trade_state'] == 'USERPAYING' || $result['trade_state'] == 10003) {
                                                                                exit(json_encode(array('success' => true, 'paywait' => true, 'orderid' => $orderid)));
                                                                            } else {
                                                                                if ($result['trade_state'] == 'SUCCESS') {
                                                                                    $data = array('openid' => $result['sub_openid'], 'ispay' => 1, 'status' => 2, 'cash_fee' => intval($result['total_fee']), 'transaction_id' => $result['out_transaction_id'], 'time_end' => strtotime($result['time_end']));
                                                                                    if ($result['trade_type'] == 'pay.weixin.micropay') {
                                                                                        if ($trade['paytype'] == 0) {
                                                                                            $data['pay1_status'] = 1;
                                                                                        }
                                                                                        if ($trade['paytype2'] == 0) {
                                                                                            $data['pay2_status'] = 1;
                                                                                        }
                                                                                    } else {
                                                                                        if ($trade['paytype'] == 1) {
                                                                                            $data['pay1_status'] = 1;
                                                                                        }
                                                                                        if ($trade['paytype2'] == 1) {
                                                                                            $data['pay2_status'] = 1;
                                                                                        }
                                                                                    }
                                                                                    pdo_update('j_hmoney_order', $data, array('id' => $trade['id']));
                                                                                    if ($trade['timecardgid'] && $trade['memberno']) {
                                                                                        $goodlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . '  WHERE id in (' . $trade['timecardgid'] . ') order by id desc ');
                                                                                        foreach ($goodlist as $row) {
                                                                                            $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_timecardorder') . '  WHERE memberid=:a and pid=:b and usetime=0 and status=0 order by id asc ', array(':a' => $trade['memberno'], ':b' => $row['goodid']));
                                                                                            if ($item) {
                                                                                                pdo_update('j_hmoney_timecardorder', array('orderid' => $orderid, 'ordergoodid' => $row['id'], 'usetime' => TIMESTAMP, 'status' => 1, 'total_fee' => $row['total_fee'], 'title' => $row['title']), array('id' => $item['id']));
                                                                                                pdo_update('j_hmoney_ordergoods', array('tcardid' => $item['id']), array('id' => $row['id']));
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                    pdo_update('j_hmoney_ordergoods', array('ispay' => 1, 'status' => 2), array('orderid' => $trade['id']));
                                                                                    exit(json_encode(array('success' => true, 'paywait' => false, 'isnew' => false, 'out_trade_no' => $trade['out_trade_no'])));
                                                                                }
                                                                            }
                                                                            exit(json_encode(array('success' => false, 'msg' => $result['trade_state'])));
                                                                        }
                                                                        exit(json_encode(array('success' => false, 'msg' => $result['message'] . $result['err_msg'])));
                                                                    } else {
                                                                        load()->func('communication');
                                                                        if ($trade['paytype'] == 0) {
                                                                            $payParama = array('appid' => $shop['appid'] ? $shop['appid'] : $cfg['appid'], 'pay_mchid' => strval($shop['pay_mchid']) ? strval($shop['pay_mchid']) : strval($cfg['pay_mchid']), 'pay_signkey' => $shop['pay_signkey'] ? $shop['pay_signkey'] : $cfg['pay_signkey'], 'outTradeNo' => strval($trade['out_trade_no']), 'pay_ip' => $shop['pay_ip'] ? $shop['pay_ip'] : $cfg['pay_ip'], 'sub_appid' => $shop['sub_appid'] ? $shop['sub_appid'] : $cfg['sub_appid'], 'sub_mch_id' => $shop['sub_mch_id'] ? $shop['sub_mch_id'] : $cfg['sub_mch_id']);
                                                                            $pageparama = array('appid' => $payParama['appid'], 'mch_id' => strval($payParama['pay_mchid']), 'out_trade_no' => $trade['out_trade_no'], 'nonce_str' => getNonceStr());
                                                                            if ($payParama['sub_appid']) {
                                                                                $pageparama['sub_appid'] = $payParama['sub_appid'];
                                                                            }
                                                                            if ($payParama['sub_mch_id']) {
                                                                                $pageparama['sub_mch_id'] = $payParama['sub_mch_id'];
                                                                            }
                                                                            $sign = MakeSign($pageparama, $payParama['pay_signkey']);
                                                                            $pageparama['sign'] = $sign;
                                                                            $xml = ToXml($pageparama);
                                                                            $response = postXmlCurl($xml, 'https://api.mch.weixin.qq.com/pay/orderquery', 10);
                                                                            $result = FromXml($response);
                                                                            if ($result['result_code'] == 'SUCCESS') {
                                                                                if ($result['trade_state'] == 'USERPAYING') {
                                                                                    exit(json_encode(array('success' => true, 'paywait' => true, 'out_trade_no' => $trade['out_trade_no'])));
                                                                                } else {
                                                                                    if ($result['trade_state'] == 'REFUND') {
                                                                                        exit(json_encode(array('success' => false, 'msg' => '已退款')));
                                                                                    } else {
                                                                                        if ($result['trade_state'] == 'NOTPAY') {
                                                                                            exit(json_encode(array('success' => false, 'msg' => '没有支付')));
                                                                                        } else {
                                                                                            if ($result['trade_state'] == 'CLOSED') {
                                                                                                exit(json_encode(array('success' => false, 'msg' => '已关闭')));
                                                                                            } else {
                                                                                                if ($result['trade_state'] == 'REVOKED') {
                                                                                                    exit(json_encode(array('success' => false, 'msg' => '已撤销')));
                                                                                                } else {
                                                                                                    if ($result['trade_state'] == 'PAYERROR') {
                                                                                                        exit(json_encode(array('success' => false, 'msg' => '支付失败(银行返回失败)')));
                                                                                                    } else {
                                                                                                        if ($result['trade_state'] == 'SUCCESS') {
                                                                                                            $data = array('openid' => $result['openid'], 'ispay' => 1, 'status' => 2, 'coupon_fee' => intval($result['coupon_fee']), 'cash_fee' => intval($result['cash_fee']), 'transaction_id' => $result['transaction_id'], 'time_end' => strtotime($result['time_end']));
                                                                                                            if ($trade['paytype'] == 0) {
                                                                                                                $data['pay1_status'] = 1;
                                                                                                            }
                                                                                                            if ($trade['paytype2'] == 0) {
                                                                                                                $data['pay2_status'] = 1;
                                                                                                            }
                                                                                                            pdo_update('j_hmoney_order', $data, array('id' => $trade['id']));
                                                                                                            if ($trade['timecardgid'] && $trade['memberno']) {
                                                                                                                $goodlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . '  WHERE id in (' . $trade['timecardgid'] . ') order by id desc ');
                                                                                                                foreach ($goodlist as $row) {
                                                                                                                    $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_timecardorder') . '  WHERE memberid=:a and pid=:b and usetime=0 and status=0 order by id asc ', array(':a' => $trade['memberno'], ':b' => $row['goodid']));
                                                                                                                    if ($item) {
                                                                                                                        pdo_update('j_hmoney_timecardorder', array('orderid' => $orderid, 'ordergoodid' => $row['id'], 'usetime' => TIMESTAMP, 'status' => 1, 'total_fee' => $row['total_fee'], 'title' => $row['title']), array('id' => $item['id']));
                                                                                                                        pdo_update('j_hmoney_ordergoods', array('tcardid' => $item['id']), array('id' => $row['id']));
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                            pdo_update('j_hmoney_ordergoods', array('ispay' => 1, 'status' => 2), array('orderid' => $trade['id']));
                                                                                                            if ($trade['ispay'] == 0) {
                                                                                                                exit(json_encode(array('success' => true, 'paywait' => false, 'isnew' => true, 'out_trade_no' => $trade['out_trade_no'])));
                                                                                                            }
                                                                                                            exit(json_encode(array('success' => true, 'paywait' => false, 'isnew' => false, 'out_trade_no' => $trade['out_trade_no'])));
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                            exit(json_encode(array('success' => false, 'msg' => $result['trade_state_desc'] . $result['err_code_des'] . $result['return_msg'])));
                                                                        } else {
                                                                            $payParama = array('app_id' => $shop['app_id'] ? $shop['app_id'] : $cfg['app_id'], 'appauthtoken' => $shop['appauthtoken'] ? $shop['appauthtoken'] : $cfg['appauthtoken'], 'alipay_cert' => $cfg['alipay_cert'], 'alipay_key' => $cfg['alipay_key'], 'alipay_store_id' => $shop['alipay_store_id'], 'alipid' => $cfg['alipid']);
                                                                            require_once '../addons/j_hmoney/plug/plug_alipay/F2fpay.php';
                                                                            $cfg = $this->module['config'];
                                                                            $f2fpay = new F2fpay();
                                                                            $response = $f2fpay->query($out_trade_no, $payParama);
                                                                            error_reporting(0);
                                                                            $results = json_decode(json_encode($response), true);
                                                                            $result = $results['alipay_trade_query_response'];
                                                                            if ($result['code'] == 10003) {
                                                                                exit(json_encode(array('success' => true, 'paywait' => true, 'msg' => '等待客户支付密码', 'out_trade_no' => $trade['out_trade_no'])));
                                                                            } else {
                                                                                if ($result['code'] == 10000) {
                                                                                    if ($result['trade_status'] == 'TRADE_SUCCESS') {
                                                                                        $data = array('ispay' => 1, 'status' => 2, 'transaction_id' => $result['trade_no'], 'cash_fee' => intval($result['receipt_amount'] * 100), 'time_end' => strtotime($result['gmt_payment']));
                                                                                        if ($trade['paytype'] == 1) {
                                                                                            $data['pay1_status'] = 1;
                                                                                        }
                                                                                        if ($trade['paytype2'] == 1) {
                                                                                            $data['pay2_status'] = 1;
                                                                                        }
                                                                                        pdo_update('j_hmoney_order', $data, array('id' => $trade['id']));
                                                                                        if ($trade['timecardgid'] && $trade['memberno']) {
                                                                                            $goodlist = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . '  WHERE id in (' . $trade['timecardgid'] . ') order by id desc ');
                                                                                            foreach ($goodlist as $row) {
                                                                                                $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_timecardorder') . '  WHERE memberid=:a and pid=:b and usetime=0 and status=0 order by id asc ', array(':a' => $trade['memberno'], ':b' => $row['goodid']));
                                                                                                if ($item) {
                                                                                                    pdo_update('j_hmoney_timecardorder', array('orderid' => $orderid, 'ordergoodid' => $row['id'], 'usetime' => TIMESTAMP, 'status' => 1, 'total_fee' => $row['total_fee'], 'title' => $row['title']), array('id' => $item['id']));
                                                                                                    pdo_update('j_hmoney_ordergoods', array('tcardid' => $item['id']), array('id' => $row['id']));
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                        pdo_update('j_hmoney_ordergoods', array('ispay' => 1, 'status' => 2), array('orderid' => $trade['id']));
                                                                                        $isnew = false;
                                                                                        if ($trade['ispay'] == 0) {
                                                                                            $isnew = true;
                                                                                        }
                                                                                        exit(json_encode(array('success' => true, 'paywait' => false, 'isnew' => $isnew, 'out_trade_no' => $trade['out_trade_no'])));
                                                                                    } else {
                                                                                        if ($result['trade_status'] == 'WAIT_BUYER_PAY') {
                                                                                            exit(json_encode(array('success' => false, 'msg' => '等待客户付款')));
                                                                                        } else {
                                                                                            if ($result['trade_status'] == 'TRADE_CLOSED') {
                                                                                                exit(json_encode(array('success' => false, 'msg' => '订单已退款')));
                                                                                            } else {
                                                                                                exit(json_encode(array('success' => false, 'msg' => $result['trade_status'])));
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                            exit(json_encode(array('success' => false, 'msg' => $result['sub_msg'])));
                                                                        }
                                                                    }
                                                                } else {
                                                                    if ($operation == 'paysuccess') {
                                                                        $orderid = $_GPC['orderid'];
                                                                        $order = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a', array(':a' => $orderid));
                                                                        if (!$order || $order['isconfirm'] || !$order['ispay']) {
                                                                            exit(json_encode(array('success' => false, 'msg' => '订单异常')));
                                                                        }
                                                                        load()->func('communication');
                                                                        $temstr = urldecode($cfg['tempmsg_payparama']);
                                                                        $tempstr = str_replace('|单号|', $order['out_trade_no'], $temstr);
                                                                        $tempstr = str_replace('|店铺|', $shop['companyname'], $tempstr);
                                                                        $tempstr = str_replace('|时间|', date('m/d H:i', $order['createtime']), $tempstr);
                                                                        $tempstr = str_replace('|处理人|', $user['realname'], $tempstr);
                                                                        $tempstr = str_replace('|总金额|', '￥' . $this->_2money($order['order_fee']), $tempstr);
                                                                        $tempstr = str_replace('|茶位|', '￥' . $this->_2money($order['teafee']), $tempstr);
                                                                        $tempstr = str_replace('|优惠|', '￥' . $this->_2money($order['discount_fee']), $tempstr);
                                                                        $tempstr = str_replace('|实付金额|', '￥' . $this->_2money($order['paid_fee']), $tempstr);
                                                                        $paystr = _getPaytpye($order['paytype'], $shopid);
                                                                        if ($order['paid_fee2']) {
                                                                            $paystr .= _getPaytpye($order['paytype'], $shopid);
                                                                        }
                                                                        $tempstr = str_replace('|支付方式|', $paystr, $tempstr);
                                                                        $tempstr = str_replace('|数量|', $order['num'], $tempstr);
                                                                        $itemary = json_decode($tempstr, true);
                                                                        $temp = array();
                                                                        foreach ($itemary as $key => $val) {
                                                                            $temp[$key] = array('value' => $val['value'], 'color' => $val['color'] ? $val['color'] : '#333333');
                                                                        }
                                                                        $temp['remark']['value'] = '详细收款情况请登录后台查看';
                                                                        $acid = pdo_fetchcolumn('SELECT acid FROM ' . tablename('account') . ' WHERE uniacid=:uniacid ', array(':uniacid' => $_W['uniacid']));
                                                                        $acc = WeAccount::create($acid);
                                                                        if ($user['openid']) {
                                                                            $acc->sendTplNotice($user['openid'], $cfg['tempmsg_pay'], $temp, '', '#FF0000');
                                                                        }
                                                                        $userlist = pdo_fetchall('SELECT openid FROM ' . tablename('j_money_user') . ' WHERE  pcate=:a and permission=3 ', array(':a' => $order['groupid']));
                                                                        if (count($userlist)) {
                                                                            foreach ($userlist as $row) {
                                                                                if ($row['openid'] != $user['openid']) {
                                                                                    $data = $acc->sendTplNotice($row['openid'], $cfg['tempmsg_pay'], $temp, '', '#FF0000');
                                                                                }
                                                                            }
                                                                        }
                                                                        if ($order['openid']) {
                                                                            $temp['remark']['value'] = '谢谢惠顾';
                                                                            $data = $acc->sendTplNotice($order['openid'], $cfg['tempmsg_pay'], $temp, '', '#FF0000');
                                                                        }
                                                                        if ($order['memberno']) {
                                                                            $memberopenid = pdo_fetchcolumn('SELECT openid FROM ' . tablename('j_hmoney_membercard') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a', array(':a' => $order['memberno']));
                                                                            $temp['remark']['value'] = '谢谢惠顾';
                                                                            if ($order['memberno']) {
                                                                                $data = $acc->sendTplNotice($order['openid'], $cfg['tempmsg_pay'], $temp, '', '#FF0000');
                                                                            }
                                                                        }
                                                                        pdo_update('j_hmoney_order', array('isconfirm' => 1), array('id' => $orderid));
                                                                        $qrcode = $this->IncreateQrcode($order['out_trade_no'], 2, 1);
                                                                        pdo_update('j_hmoney_order', array('qrcode' => $qrcode), array('id' => $orderid));
                                                                        exit(json_encode(array('success' => true)));
                                                                    } else {
                                                                        if ($operation == 'getorderlist') {
                                                                            $datetype = intval($_GPC['datetype']);
                                                                            $keyword = trim($_GPC['keyword']);
                                                                            switch ($datetype) {
                                                                                case 0:
                                                                                    break;
                                                                                case 1:
                                                                                    $condition = ' and createdate=\'' . date('Y-m-d') . '\' ';
                                                                                    break;
                                                                                case 2:
                                                                                    $condition = ' and createdate=\'' . date('Y-m-d', strtotime('-1 day')) . '\' ';
                                                                                    break;
                                                                                case 3:
                                                                                    $condition = ' and createdate>=\'' . date('Y-m-d', strtotime('-3 day')) . '\' ';
                                                                                    break;
                                                                                case 4:
                                                                                    $condition = ' and createdate>=\'' . date('Y-m-d', strtotime('-7 day')) . '\' ';
                                                                                    break;
                                                                                case 5:
                                                                                    $condition = ' and date_format(createdate,\'%y-%m\')=\'' . date('y-m') . '\' ';
                                                                                    break;
                                                                            }
                                                                            if ($keyword) {
                                                                                $condition = ' and out_trade_no=\'' . $keyword . '\' ';
                                                                            }
                                                                            $condition .= ' and groupid =\'' . $shopid . '\' ';
                                                                            $sql = 'SELECT id,out_trade_no,createtime,serialnum FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' ' . $condition . ' order by id desc ';
                                                                            $list = pdo_fetchall($sql);
                                                                            exit(json_encode(array('success' => true, 'list' => $list)));
                                                                        } else {
                                                                            if ($operation == 'gethistoryview') {
                                                                                $tid = $_GPC['id'];
                                                                                $order = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a', array(':a' => $tid));
                                                                                if ($order['ispay']) {
                                                                                    if ($order['paid_fee2']) {
                                                                                    } else {
                                                                                        _getPaytpye($order['paytype'], $shopid);
                                                                                    }
                                                                                    $order['paytypestr'] = _getPaytpye($order['paytype'], $shopid) . '/' . _getPaytpye($order['paytype2'], $shopid);
                                                                                }
                                                                                $goods = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and orderid=:a order by id asc', array(':a' => $order['id']));
                                                                                exit(json_encode(array('success' => true, 'trade' => $order, 'table' => $table, 'goods' => $goods)));
                                                                            } else {
                                                                                if ($operation == 'getordergood') {
                                                                                    $gid = $_GPC['id'];
                                                                                    $goods = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_ordergoods') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a ', array(':a' => $gid));
                                                                                    if ($goods['ispay'] != 1) {
                                                                                        exit(json_encode(array('success' => false, 'msg' => '该商品不能退款')));
                                                                                    }
                                                                                    $order = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_order') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=:a', array(':a' => $goods['orderid']));
                                                                                    $maxfee = $goods['price'];
                                                                                    if ($order['discount'] != '100%') {
                                                                                        str_replace('%', '', $order['discount']);
                                                                                        $maxfee = $this->_2money($goods['price'] * str_replace('%', '', $order['discount']));
                                                                                    }
                                                                                    exit(json_encode(array('success' => true, 'maxfee' => $maxfee, 'good' => $goods, 'paytype' => $order['paytype'], 'paytypestr' => _getPaytpye($order['paytype'], $shopid))));
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

if ($operation == 'refundorder') {
    $orderid = $_GPC["orderid"];
    $remark = $_GPC["remark"];
    $paytype = $_GPC["paytype"];
    $gid = $_GPC["id"];
    $num = $_GPC["num"];
    $isgetback = intval($_GPC["isgetback"]);
    $fee = intval($_GPC["fee"] * 100);
    if (!$gid) {
        $order = pdo_fetch("SELECT * FROM " . tablename('j_hmoney_order') . " WHERE weid = '" . $_W['uniacid'] . "' and id=:a", array(":a" => $orderid));
        if ($order['ispay'] != 1) die(json_encode(array("success" => true, "msg" => "订单异常不能退款")));
        if ($order['paid_fee'] == 0) die(json_encode(array("success" => true, "msg" => "订单已退款")));
        if ($order['paid_fee'] < $fee) die(json_encode(array("success" => true, "msg" => "退款金额大于支付金额")));
        $result = $this->fun_refunder(1, $orderid, $userid, $shopid, $remark, 0, $fee, $paytype, $isgetback);
    } else {
        $goods = pdo_fetch("SELECT * FROM " . tablename('j_hmoney_ordergoods') . " WHERE weid = '{$_W['uniacid']}' and id=:a ", array(":a" => $gid));
        if ($goods['ispay'] != 1) die(json_encode(array("success" => true, "msg" => "商品异常不能退款")));
        if ($goods['total_fee'] == 0) die(json_encode(array("success" => true, "msg" => "商品已退款")));
        $result = $this->fun_refunder(2, $gid, $userid, $shopid, $remark, $num, $fee, $paytype, $isgetback);
    }
    die(json_encode($result));
}

include $this->template($cfg['floder'] . '/index');